package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.customView.DoubleInputDialog;
import com.dlc.felear.xiaoensale.dao.SfPositionDao;
import com.dlc.felear.xiaoensale.entity.SfPosition;
import com.dlc.felear.xiaoensale.entity.TempSfPosition;

import java.util.ArrayList;
import java.util.List;


public class SfPositionItemAdapter extends BaseAdapter implements View.OnClickListener,DoubleInputDialog.DoubleDialogCallback{

    private static final String TAG = "SfPositionItemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<TempSfPosition> data;
    private SfPositionDao sfPositionDao;
    private int addr;
    private SfPosition sfPosition;
    private DoubleInputDialog doubleInputDialog;

    public SfPositionItemAdapter(Context context,int addr) {
        sfPositionDao=new SfPositionDao(context);
        this.context = context;
        this.addr = addr;
        doubleInputDialog=new DoubleInputDialog(context,this);
        doubleInputDialog.setTips("步数:","口令:");
        doubleInputDialog.setHintMsg("请输入电机转动步数","请输入密码口令");
        doubleInputDialog.setEtInputType(InputType.TYPE_NUMBER_FLAG_SIGNED ,
                InputType.TYPE_NUMBER_FLAG_SIGNED);
        data=new ArrayList<>();
        initData();

    }

    private void initData(){
        sfPosition=sfPositionDao.queryForId(addr);
        if(sfPosition==null){
            return;
        }
        data.clear();
        Log.e(TAG,"size:"+sfPositionDao.queryForAll().size());
        data.add(new TempSfPosition("位置1",sfPosition.getPosition1()));
        data.add(new TempSfPosition("位置2",sfPosition.getPosition2()));
        data.add(new TempSfPosition("位置3",sfPosition.getPosition3()));
        data.add(new TempSfPosition("位置4",sfPosition.getPosition4()));
        data.add(new TempSfPosition("位置5",sfPosition.getPosition5()));
        data.add(new TempSfPosition("位置6",sfPosition.getPosition6()));
        data.add(new TempSfPosition("位置7",sfPosition.getPosition7()));
        data.add(new TempSfPosition("位置8",sfPosition.getPosition8()));
        data.add(new TempSfPosition("位置9",sfPosition.getPosition9()));
        data.add(new TempSfPosition("位置10",sfPosition.getPosition10()));
        data.add(new TempSfPosition("位置11",sfPosition.getPosition11()));
        data.add(new TempSfPosition("位置12",sfPosition.getPosition12()));
        data.add(new TempSfPosition("位置13",sfPosition.getPosition13()));
        data.add(new TempSfPosition("位置14",sfPosition.getPosition14()));
        data.add(new TempSfPosition("位置15",sfPosition.getPosition15()));
        data.add(new TempSfPosition("位置16",sfPosition.getPosition16()));
    }



    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public TempSfPosition getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_sf_p_item, null);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_value = convertView.findViewById(R.id.tv_value);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_name.setText(data.get(position).getName());
        holder.tv_value.setText(data.get(position).getPosition()+"");
        holder.tv_value.setOnClickListener(this);
        holder.tv_value.setTag(R.id.tv_value,position);
        return convertView;
    }

    @Override
    public void onClick(View v) {
        int p=(int)v.getTag(R.id.tv_value);
        doubleInputDialog.showDialog(p);
    }

    @Override
    public void doubleDialogInputResult(int value1, int value2, int id) {
        if(value2==666888){
            switch (id){
                case 0:
                    sfPosition.setPosition1(value1);
                    break;
                case 1:
                    sfPosition.setPosition2(value1);
                    break;
                case 2:
                    sfPosition.setPosition3(value1);
                    break;
                case 3:
                    sfPosition.setPosition4(value1);
                    break;
                case 4:
                    sfPosition.setPosition5(value1);
                    break;
                case 5:
                    sfPosition.setPosition6(value1);
                    break;
                case 6:
                    sfPosition.setPosition7(value1);
                    break;
                case 7:
                    sfPosition.setPosition8(value1);
                    break;
                case 8:
                    sfPosition.setPosition9(value1);
                    break;
                case 9:
                    sfPosition.setPosition10(value1);
                    break;
                case 10:
                    sfPosition.setPosition11(value1);
                    break;
                case 11:
                    sfPosition.setPosition12(value1);
                    break;
                case 12:
                    sfPosition.setPosition13(value1);
                    break;
                case 13:
                    sfPosition.setPosition14(value1);
                    break;
                case 14:
                    sfPosition.setPosition15(value1);
                    break;
                case 15:
                    sfPosition.setPosition16(value1);
                    break;
            }
            sfPositionDao.add(sfPosition);
            initData();
            notifyDataSetChanged();
            showToast("修改成功，需重启才生效");
        }else {
            showToast("输入的口令错误");
        }
    }

    static class ViewHolder {
        TextView tv_name, tv_value;
    }

    private void showToast(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

}